<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-6-3
 * Func   彩种管理
 */
defined('IN_YuLin') || exit('NO PERMIT!');
$table = Table('lottery_category');
$page  = 'lottery/category';

if (IS_POST){
    $operation = NoBadStr($_REQUEST['operation']);
    $flag      = '';
    $data = array(
        'name'        => NoBadStr($_REQUEST['name']),
        'pid'         => NoBadStr($_REQUEST['pid']),
        'sort'        => NoBadStr($_REQUEST['sort']) ? NoBadStr($_REQUEST['sort']) : 0,
        'status'      => NoBadStr($_REQUEST['status']) ? NoBadStr($_REQUEST['status']) : 0,
        'description' => NoBadStr($_REQUEST['description']) ? NoBadStr($_REQUEST['description']) : 0,
        'content'     => NoBadStr($_REQUEST['content']) ? NoBadStr($_REQUEST['content']) : 0,
        'tags'        => NoBadStr($_REQUEST['tags']) ? NoBadStr($_REQUEST['tags']) : 0,
        'sname'       => NoBadStr($_REQUEST['sname']) ? NoBadStr($_REQUEST['sname']) : 0,
    );
    $data['description'] = addslashes(htmlspecialchars_decode($data['description']));
    $data['content']     = addslashes(htmlspecialchars_decode($data['content']));
    switch ($operation){
        case 'add':
            $id = NoBadStr($_REQUEST['id']);
            if ($id){
                // 修改
                $execSql = 'UPDATE ' . $table . ' SET ' . CreateUpdateSql($data) . ' WHERE id = ' . $id;
            }else{
                $execSql = 'INSERT INTO ' . $table . ' SET ' . CreateUpdateSql($data);
            }
            $flag   = $db->exec($execSql);
            break;
    }

    if ($flag){
        ShowMsg('操作成功',U('lottery/category'));
        // AjaxReturn(['error'=>'0','msg'=>'操作成功']);
    }else{
        ShowMsg('操作成功',-1);
        // AjaxReturn(['error'=>'1','msg'=>'操作失败']);
    }
}

// 载入所有的menu，并且设置好树形结构
$menuTreeData = getMenuTree($db);
switch ($a){
    case 'add':
        // 是否设置子级
        $id   = NoBadStr($_REQUEST['id']);
        $page = 'lottery/category_add';
        break;
    case 'edit':
        $id   = NoBadStr($_REQUEST['id']);
        $infoSql = 'SELECT * FROM ' . $table . ' WHERE id = ' . $id;
        $info = $db->getrow($infoSql);
        $info['description'] = $info['description'];
        $info['content']     = $info['content'];
        $page = 'lottery/category_edit';
        break;
    case 'del':
        $id = intval($_GET['id']);
        $row = $db->affected_rows("SELECT id FROM ".Table('lottery_category')." WHERE id = ".$id);
        if($row){
            $db->exec("DELETE FROM ".Table('lottery_category')." WHERE id = ".$id);
            AjaxReturn(['error'=>'0','msg'=>'操作成功']);
        }else{
            AjaxReturn(['error'=>'1','msg'=>'操作失败']);            
        }
        break;
    default :
        break;
}

$tpl->display($page);



function getMenuTree($db){
    // 查询所有的栏目信息，
    $sqlGetMenu = 'SELECT * FROM ' . Table('lottery_category') . ' ORDER BY id ASC';
    $menuList   = $db->getall($sqlGetMenu);
    $menuIdKey  = array();
    foreach ($menuList as $mk => $mv){
        $menuIdKey[$mv['id']] = $mv;
    }
    // menu信息准备生成树状结构
    // 递归获取级联数据
    $arr              = array();
    foreach ($menuList as $k => $v){
        if ($v['pid'] == 0){
            $v['lv'] = '1';
            $arr[]   = $v;
            getChild($menuList, $v['id'], $arr, 1);
        }
    }
    return $arr;
}

function getChild($menuList, $id, &$arr, $lv){
    $lv++;
    foreach ($menuList as $k => $v){
        if ($v['pid'] == $id){
            $v['lv']   = $lv;
            $line      = '';
            for($i = 1; $i < $lv; $i++){
                $line  = $line . '－－';
            }
            $v['line'] = $line;
            $arr[]     = $v;
            // 检查是否还有子项
            getChild($menuList, $v['id'], $arr, $lv);
        }
    }
}
